package com.sogou.ime.ui.settings.dynamic;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParser;

import android.content.res.XmlResourceParser;
import android.util.Xml;

import com.sogou.ime.ui.settings.dynamic.CandidateKeyboardLocation.CandCharacterListMode;
import com.sogou.ime.ui.settings.dynamic.KeyboardLayoutConfiguration.KeyInfo;


public class KeyboardCandidateCharConfig {
	public static final String CharacterTagName = "character";
	public static final String SymbolTagName = "symbol";
	public static final int defaultBaseKeyCode = 10000; 
	
	public ArrayList<Character> candCharList = new ArrayList<Character>();
	public ArrayList<Character> candSymbolList = new ArrayList<Character>();
	
	public boolean parseXmlFile(XmlResourceParser xrp)
	{
		try {
			while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT)
			{
				if (xrp.getEventType() == XmlResourceParser.START_TAG) 
				{
					String key = xrp.getName();
					if (xrp.next() == XmlResourceParser.TEXT)
					{
						String value = xrp.getText();
						char[] charArray = value.toCharArray();
						if (charArray.length == 0)	continue;
						char addChar = charArray[0];
						
						if (key.equals(CharacterTagName))
						{
							candCharList.add(new Character(addChar));
						} else if (key.equals(SymbolTagName))
						{
							candSymbolList.add(addChar);
						}
					}
					else {
						continue;
					}
				}
				xrp.next();
			}
		} catch (Exception e) {
			return false;
		}

		return true;
	}
	
	public boolean createCandCharKeyCode(HashMap<String, KeyInfo> keyInfoMap)
	{
		int index = 0;
		for (Character ch : candCharList)
		{
			if (keyInfoMap.containsKey(ch.toString()))	continue;
			KeyInfo keyInfo = new KeyInfo();
			keyInfo.keylabel = ch.toString();
			keyInfo.keycode = defaultBaseKeyCode+index;
			keyInfoMap.put(ch.toString(), keyInfo);
			index++;
		}

		for (Character ch : candSymbolList)
		{
			if (keyInfoMap.containsKey(ch.toString()))	continue;
			KeyInfo keyInfo = new KeyInfo();
			keyInfo.keylabel = ch.toString();
			keyInfo.keycode = defaultBaseKeyCode+index;
			keyInfoMap.put(ch.toString(), keyInfo);
			index++;
		}
		
		return true;
	}
	
	public boolean parseSdCardXmlFile(String fileName)
	{
		XmlPullParser parser = Xml.newPullParser();
		InputStream ins = GlobalUtils.getInputStreamFromSdCardFile(fileName);
		if (ins == null)
		{
			return false;
		}
		
		try {
			parser.setInput(ins, "UTF-8");
			while (parser.getEventType() != XmlResourceParser.END_DOCUMENT)
			{
				if (parser.getEventType() == XmlResourceParser.START_TAG) 
				{
					String key = parser.getName();
					if (parser.next() == XmlResourceParser.TEXT)
					{
						String value = parser.getText();
						char[] charArray = value.toCharArray();
						if (charArray.length == 0)	continue;
						char addChar = charArray[0];
						
						if (key.equals(CharacterTagName))
						{
							candCharList.add(new Character(addChar));
						} else if (key.equals(SymbolTagName))
						{
							candSymbolList.add(addChar);
						}
					}
					else {
						continue;
					}
				}
				parser.next();
			}
		} catch (Exception e) {
			return false;
		}

		return true;
	}
	
	public boolean sortCandKeyboardCharacters(CandCharacterListMode candCharacterListMode)
	{
		if (candCharacterListMode == CandCharacterListMode.Character)
		{
			candCharList = sortCharacterList(candCharList);
		} else if (candCharacterListMode == CandCharacterListMode.Symbol)
		{
			candSymbolList = sortCharacterList(candSymbolList);
		} else {
			return false;
		}
		
		return true;
	}
	
	public ArrayList<Character> sortCharacterList(ArrayList<Character> charArrayList)
	{
		ArrayList<Character> sortedCharArraylist = new ArrayList<Character>();
		TreeMap<Character, Character> map = new TreeMap<Character, Character>();      

	    for(Character oneChar : charArrayList) {
	        map.put(oneChar, oneChar);
	    }

	    Collection<Character> col = map.values();
	    Iterator<Character> it = col.iterator();
	    while(it.hasNext()) {
	    	Character oneChar = it.next();
//	    	Log.d("debug", "sortCharacterList onechar="+oneChar);
	    	sortedCharArraylist.add(oneChar);
	    }
	    
		return sortedCharArraylist;
	}
}
